package com.hxzy.login_2;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by Administrator on 2016/9/18.
 */
public class TestCountdownBtnActivity extends AppCompatActivity {
    private Button button;
    private int num = 10;
    private static final int UPDATE_BTN_TEXT = 0;
    /*private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == UPDATE_BTN_TEXT) {
                int seconds = (int) msg.obj; button.setText("已发送" + seconds + "秒");
            }
        }
    };*/

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_2);

        button = (Button) findViewById(R.id.cbtn);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                button.setEnabled(false); new CountDownThread(5).start();
            }
        });
    }
//                new CountDownThead(60).start();

//                new CountDownThead(60).start();
//                countdown();
//                new CountdownThead2(30).start();
                /*button.setEnabled(false);
                CountDownTimer countDownTimer = new CountDownTimer(5000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        button.setText("已发送" + millisUntilFinished / 1000 + "s");
                    }

                    @Override
                    public void onFinish() {
                        button.setText("重新发送"); button.setEnabled(true);
                    }
                }.start();
            }
        });*/


    /*public void countdown(){
        new Timer("").schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        button.setText("已发送"+num+"s");
                    }
                });
                if(num > 0)countdown();
                num--;
            }
        },1000);
    }*/
    class CountDownThread extends CountDownTimer {
        private int startSeconds;

        public CountDownThread(int secondsInFuture) {
            super(secondsInFuture * 1000, 1000); this.startSeconds = secondsInFuture;
        }

        @Override
        public void onTick(long millisUntilFinished) {
            startSeconds--;
            button.setText("已发送" + startSeconds + "s");
        }

        @Override
        public void onFinish() {

            button.setText("重新发送"); button.setEnabled(true);
        }
    }
}
